//
//  NHT_AMap.swift
//  NHT_AMap
//
//  Created by NHT on 2024/6/27.
//

import UIKit
import CoreLocation

public class NHT_AMap: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    ///是否已经加载到superview
    var didAddToSuperView = false;
    public override func didMoveToSuperview() {
        if self.superview != nil && !didAddToSuperView{
            didAddToSuperView = true;
            self.testBlock?("----NHT_AMap---didMoveToSuperview");
        }
        if(didAddToSuperView && self.superview == nil){
            mapView.removeFromSuperview()
        }
    }
    public override func layoutSubviews() {
        self.mapView.frame = self.bounds;
        self.testBlock?(["----NHT_AMap---layoutSubviews",self.mapView.frame.size]);
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var mapView: NHTMapView = {
      var map = NHTMapView.init()
        map.testBlock = {res in
            self.testBlock?(res);
        }
        map.SelectLocationChangeBlock = {res in
            self.SelectLocationChangeBlock?(res);
        }
        map.LocationResultBlock = {res in
            self.LocationResultBlock?(res);
        }
        self.addSubview(map);
        self.testBlock?("----NHT_AMap---创建map容器-2");
        return map
    }()
    
    ///选择位置回调
    @objc open var SelectLocationChangeBlock: (([String:Any])->Void)?
    /// 定位结果回调
    @objc open var LocationResultBlock: (([String:Any])->Void)?
    
    /// 更改地图中心点
    @objc open func setMapCenter(latitude:Float,longitude:Float) -> Void {
        self.testBlock?("----NHT_AMap---setMapCenter");
        mapView.setMapCenter(latitude: latitude, longitude: longitude)
    }
    ///设置地图api
    @objc open func setMapApi(apiKey:String) -> Void {
        self.testBlock?("----NHT_AMap---setMapApi");
        mapView.apiKey = apiKey
    }
    /// 添加marker，地图打点
    @objc open  func addMarker(latitude:Float,longitude:Float) -> Void {
        self.testBlock?("----NHT_AMap---addMarker");
        self.mapView.addMarker(latitude: latitude, longitude: longitude)
    }
    
    ///获取并显示当前位置
    @objc open  func startLocation() -> Void {
        self.testBlock?("----NHT_AMap---startLocation");
        self.mapView.startLocation()
    }
    /// 停止定位
    @objc open func stopLocation() -> Void {
        self.testBlock?("----NHT_AMap---stopLocation");
        self.mapView.stopLocation()
    }
    
    ///设置地图是否可以---手势滑动
    @objc open  func setScrollGesturesEnabled(enabled:Bool) -> Void {
        self.testBlock?("----NHT_AMap---setScrollGesturesEnabled");
        self.mapView.setScrollGesturesEnabled(enabled: enabled)
    }
    
    ///设置地图是否可以---手势缩放
    @objc open func setZoomControlsEnabled(enabled:Bool) -> Void {
        self.testBlock?("----NHT_AMap---setZoomControlsEnabled");
        self.mapView.setZoomControlsEnabled(enabled: enabled)
    }
    ///是否允许选择位置，默认为否
    @objc open  func setEnableFetchGis(enabled:Bool) -> Void {
        self.testBlock?("----NHT_AMap---setEnableFetchGis");
        self.mapView.enableFetchGis = enabled
    }
    
    /// 测试的回调
    @objc open var testBlock: ((Any)->Void)?
}

